package com.fingard.dsp.bank.directbank.cib01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

@Component("CIB01BatPayRec")
public class CIB01BatPayRec extends CIB01Base implements IBatPayRec {
	/**
	 * 批量代付提交
	 * @param p_reqBat
	 * @return
	 * @throws Exception
	 */
	@Override
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_reqBat) throws Exception {
		// 返回ATS的对象
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
		try {
			XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
			// 写入通用的头部节点
			writeCommonBatPayHeaderEn(xmlWriter);
			xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
			xmlWriter.writeStartElement(CIB01TransType.P1988);
			xmlWriter.writeElementString("TRNUID", p_reqBat.reqBatchNO);
			xmlWriter.writeStartElement("RQBODY");
			xmlWriter.writeElementString("TITLE", "批量支付");
			xmlWriter.writeStartElement("ACCTFROM");
			xmlWriter.writeElementString("ACCTID", p_reqBat.ownAct);
			xmlWriter.writeElementString("NAME", p_reqBat.ownActName);
			//xmlWriter.writeElementString("BANKDESC", "");
			String CITY = p_reqBat.ownAreaName;
			// 格式化区域字段，例如：北京市_北京市取值北京
			if (CITY.indexOf("_") > 0) {
				CITY = CITY.split("_")[1];
				if (CITY.length() > 2 && CITY.substring(CITY.length() - 2, CITY.length()).equals("市")) {
					CITY = CITY.substring(0, CITY.length() - 2);
				} else if (CITY.length() > 2 && CITY.substring(CITY.length() - 2, CITY.length()).equals("县")) {
					CITY = CITY.substring(0, CITY.length() - 2);
				}
			}
			xmlWriter.writeElementString("CITY", CITY);
			xmlWriter.writeEndElement();
			xmlWriter.writeElementString("BIZTYPE", "0");
			xmlWriter.writeElementString("TOTALCOUNT", p_reqBat.totalNum);
			xmlWriter.writeElementString("TOTALAMOUNT", p_reqBat.totalAmt);
			xmlWriter.writeElementString("CHEQUENUM", "");
			xmlWriter.writeElementString("CURSYM", "RMB");
			xmlWriter.writeElementString("PURPOSE", p_reqBat.abstractATS);
			Date tmpDateNow = new Date();
			xmlWriter.writeElementString("DTDUE", Format.Date10Format.format(tmpDateNow));
			xmlWriter.writeElementString("MEMO", p_reqBat.abstractATS);
			// 写入包体
			int tmpCount = 0;
			while (p_reqBat.readRecord()) {
				tmpCount++;
				xmlWriter.writeStartElement("XFERINFO");
				xmlWriter.writeElementString("INDX", tmpCount + "");
				String INTERBANK = (p_reqBat.readedDetail.sameBnk.equals("1") ? ("Y") : ("N"));
				String LOCAL = (p_reqBat.readedDetail.sameCity.equals("1") ? ("Y") : ("N"));
				xmlWriter.writeStartElementByAttr("ACCTTO ",
						"<ACCTTO#INTERBANK=\"" + INTERBANK + "\"#LOCAL=\"" + LOCAL + "\" >");
				xmlWriter.writeElementString("ACCTID", p_reqBat.readedDetail.recAct);
				xmlWriter.writeElementString("NAME", p_reqBat.readedDetail.recName);
				xmlWriter.writeElementString("BANKDESC", p_reqBat.readedDetail.recBankLocation);
				xmlWriter.writeElementString("BANKNUM", p_reqBat.readedDetail.cnapsCode);
				CITY = p_reqBat.readedDetail.recAreaName;
				// 格式化区域字段，例如：北京市_北京市取值北京
				if (CITY.indexOf("_") > 0) {
					CITY = CITY.split("_")[1];
					if (CITY.length() > 2 && CITY.substring(CITY.length() - 2, CITY.length()).equals("市")) {
						CITY = CITY.substring(0, CITY.length() - 2);
					} else if (CITY.length() > 2 && CITY.substring(CITY.length() - 2, CITY.length()).equals("县")) {
						CITY = CITY.substring(0, CITY.length() - 2);
					}
				}
				xmlWriter.writeElementString("CITY", CITY);
				xmlWriter.writeEndElement();
				xmlWriter.writeElementString("TRNAMT", p_reqBat.readedDetail.payAmount);
				xmlWriter.writeElementString("USE", p_reqBat.readedDetail.purpose);
				xmlWriter.writeElementString("REMARK", p_reqBat.readedDetail.askNumber);
				xmlWriter.writeEndElement();// 写入报文完成
			}
			xmlWriter.writeEndDocument();// 写入报文完成
			String xmlSend = writeContentLength(xmlWriter);
			// 测试代码：打印报文
			// System.out.println("dsp接收报文："+xmlSend);//打印返回报文
			String[] tmpStrRet = sendToBank(xmlSend);// 发送银行

			if (tmpStrRet[0].length() == 0) {
				String returnXml = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<FOX>"), tmpStrRet[1].length());
				StringBuilder tmpSbMsg = new StringBuilder();
				// 解析银行返回
				XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
				if (tmpBkReader != null) {
					String retCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/CODE");
					String errorCode=tmpBkReader.getFirstText("/FOX/SIGNONMSGSRSV1/SONRS/STATUS/CODE");
					if (retCode!=null&&retCode.equals("0")) {
						String tmpRetCode = tmpBkReader.getFirstText(
								"/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/RSBODY/XFERPRCSTS/XFERPRCCODE");
						retAts.respCode = "0";
						retAts.respInfo = tmpBkReader.getFirstText(
								"/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/MESSAGE")+"+"+tmpBkReader.getFirstText(
										"/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/RSBODY/XFERPRCSTS/MESSAGE");
						retAts.transState = tmpBkReader.getFirstText(
								"/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/SEVERITY")+"+"+tmpRetCode;
						retAts.payInfoCode = retAts.transState;
						retAts.reqSeqID = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/TRNUID");
					}else if(errorCode!=null){
						retAts.respCode="0";
						retAts.respInfo=tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/MESSAGE");
						retAts.payInfoCode=tmpBkReader.getFirstText("/FOX/SIGNONMSGSRSV1/SONRS/STATUS/SEVERITY")+"+"+tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/CODE");
						retAts.transState=retAts.payInfoCode;
						retAts.reqSeqID = p_reqBat.reqBatchNO;
					} 
					else {
						retAts.respCode = "-1";
						retAts.respInfo = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/MESSAGE");
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			} else if (tmpStrRet[0].equals(ConnState.FAIL)) {
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
				retAts.reqSeqID = p_reqBat.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = tmpStrRet[0];
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}

		} catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.toString();
			e.printStackTrace();
		}
		if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
			retAts.reqSeqID = p_reqBat.reqBatchNO;
		}
		retAts.writeRetToATS();
		return retAts;
	}

	/**
	 * 批量代扣提交
	 * @param p_reqBat
	 * @return
	 * @throws Exception
	 */
	@Override
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_reqBat) throws Exception {
		// 返回ATS的对象
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
		try {
			XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
			// 写入通用的头部节点
			writeCommonBatPayHeaderEn(xmlWriter);
			xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
			xmlWriter.writeStartElement(CIB01TransType.P9188);
			xmlWriter.writeElementString("TRNUID", p_reqBat.reqBatchNO);
			xmlWriter.writeStartElement("RQBODY");
			xmlWriter.writeElementString("ACCTID", p_reqBat.ownAct);
			xmlWriter.writeElementString("FIRMCODE", getBankConfig().getValueAsString("FIRMCODE"));
			String bizCode = getBankConfig().getMapValueByKey("BIZCODE", p_reqBat.useType);
			xmlWriter.writeElementString("BIZCODE", bizCode);// 业务编码
			String limitdays = getBankConfig().getValueAsString("LIMITDAYS");
			if (limitdays.equals("1") || limitdays.equals("2") || limitdays.equals("3")) {
				xmlWriter.writeElementString("LIMITDAYS", limitdays);// 回款期限
			} else {
				xmlWriter.writeElementString("LIMITDAYS", "3");// 回款期限 默认为3
			}
			xmlWriter.writeElementString("TITLE", "批量代收");// 标题
			xmlWriter.writeElementString("MEMO", p_reqBat.abstractATS);// 批次备注
			xmlWriter.writeElementString("TRNTYPE", "1");
			int tmpCount = 0;
			while (p_reqBat.readRecord()) {
				tmpCount++;
				xmlWriter.writeStartElement("GATHERINFO");
				xmlWriter.writeElementString("INDX", tmpCount + "");
				xmlWriter.writeElementString("CONTRACTID", getBankConfig().getValueAsString("CONTRACTID"));
				xmlWriter.writeStartElement("ACCTFROM");
				xmlWriter.writeElementString("ACCTID", p_reqBat.readedDetail.payAct);
				xmlWriter.writeElementString("NAME", p_reqBat.readedDetail.payName);
				xmlWriter.writeElementString("BANKNUM", p_reqBat.readedDetail.cnapsCode);
				xmlWriter.writeElementString("BANKDESC", p_reqBat.readedDetail.payBankLocation);
				xmlWriter.writeEndElement();
				xmlWriter.writeElementString("PAYMODE", "2");// 暂时写死，同行
				xmlWriter.writeElementString("APPLYAMT", p_reqBat.readedDetail.payAmount);
				xmlWriter.writeElementString("PURPOSE", p_reqBat.readedDetail.purpose);
				xmlWriter.writeElementString("BIZCODE0", bizCode);
				xmlWriter.writeElementString("BIZCODE1", bizCode);
				xmlWriter.writeElementString("MEMO", p_reqBat.readedDetail.askNumber);
			}
			xmlWriter.writeEndDocument();// 写入报文完成
			String xmlSend = writeContentLength(xmlWriter);
			// 测试代码：打印报文
			// System.out.println("dsp接收报文："+xmlSend);//打印返回报文
			String[] tmpStrRet = sendToBank(xmlSend);// 发送银行

			if (tmpStrRet[0].length() == 0) {
				String returnXml = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<FOX>"), tmpStrRet[1].length());
				StringBuilder tmpSbMsg = new StringBuilder();
				// 解析银行返回
				XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
				if (tmpBkReader != null) {
					String retCode = tmpBkReader
							.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/STATUS/CODE");
					String errorCode = tmpBkReader.getFirstText("/FOX/SIGNONMSGSRSV1/SONRS/STATUS/CODE");
					if (retCode != null && retCode.equals("0")) {
						retAts.respCode = "0";
						retAts.respInfo = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/STATUS/MESSAGE") + "+"
								+ tmpBkReader.getFirstText(
										"/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/RSBODY/XFERPRCSTS/MESSAGE");
						retAts.transState = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/STATUS/SEVERITY") + "+"
								+ tmpBkReader.getFirstText(
										"/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/RSBODY/XFERPRCSTS/XFERPRCCODE");
						retAts.payInfoCode = tmpBkReader.getFirstText(
										"/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/RSBODY/XFERPRCSTS/MESSAGE");
						retAts.reqSeqID = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/TRNUID");
					}else if(errorCode!=null){
						retAts.respCode="-1";
						retAts.respInfo=tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/STATUS/MESSAGE");
						retAts.payInfoCode=errorCode+"+"+tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/STATUS/CODE");
						retAts.transState=retAts.payInfoCode;
					} 
					else {
						retAts.respCode = "-1";
						retAts.respInfo = tmpSbMsg.toString();
					}
				} else if (tmpStrRet[1].indexOf("Connection refused") >= 0) {
					retAts.respCode = "0";
					retAts.respInfo = tmpStrRet[1];
					retAts.reqSeqID = p_reqBat.reqBatchNO;
					retAts.transState = PaymentState.FAIL_Batch;
					retAts.payInfoCode = "E9999";
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			} else if (tmpStrRet[0].equals(ConnState.FAIL)) {
				retAts.respCode = "0";
				retAts.respInfo = tmpStrRet[1];
				retAts.reqSeqID = p_reqBat.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = tmpStrRet[0];
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}

		} catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.toString();
			e.printStackTrace();
		}
		if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
			retAts.reqSeqID = p_reqBat.reqBatchNO;
		}
		retAts.writeRetToATS();
		return retAts;
	}

	/**
	 * 批量代付查询
	 * @param p_reqQryBat
	 * @return
	 * @throws Exception
	 */
	@Override
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_reqQryBat) throws Exception {
		// 返回ATS的对象
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
		retAts.respCode = "";

		XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
		writeCommonQryHeaderEn(xmlWriter, "encoding=\"gb2312\"");
		xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
		xmlWriter.writeStartElement(CIB01TransType.Q1988);
		xmlWriter.writeElementString("TRNUID", p_reqQryBat.reqBatchNO);
		xmlWriter.writeEndDocument();
		String xmlSend = writeContentLength(xmlWriter);
		// 测试代码：打印报文
		// System.out.println("dsp接收报文：" + xmlSend);// 打印返回报文
		String[] tmpStrRet = sendToBank(xmlSend);// 发送银行
		try {
			if (tmpStrRet[0].length() == 0) {
				String returnXml = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<FOX>"), tmpStrRet[1].length());
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
				if (tmpBkReader != null) {
					String retCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/CODE");
					String errorCode=tmpBkReader.getFirstText("/FOX/SIGNONMSGSRSV1/SONRS/STATUS/CODE");
					if (retCode!=null&&retCode.equals("0")) {
						retAts.respCode = "0";
						String tranStt = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/RSBODY/STATUS");
						if (tranStt != null) {
							retAts.respInfo = tmpBkReader.getFirstText(
									"/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/RSBODY/XFERPRCSTS/MESSAGE");
							retAts.transState = tmpBkReader
									.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/STATUS/SEVERITY") + "+"
									+ tmpBkReader.getFirstText(
											"/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/RSBODY/XFERPRCSTS/XFERPRCCODE");
							retAts.payInfoCode = tmpBkReader.getFirstText(
									"/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/RSBODY/XFERPRCSTS/XFERPRCCODE");
							retAts.reqSeqID = tmpBkReader
									.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHGATHERTRNRS/TRNUID");
							retAts.writeBeforeDetail();
							retAts.writeAfterDetail();
							return retAts;
						}
						retAts.payInfoCode = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/RSBODY/XFERPRCSTS/MESSAGE");
						retAts.reqSeqID = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/TRNUID");
						retAts.respInfo = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/MESSAGE");
						retAts.transState=retCode+"+"+tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/RSBODY/XFERPRCSTS/XFERPRCCODE");

						String payAct=tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/RSBODY/ACCTFROM/ACCTID");
						retAts.writeBeforeDetail();
						while (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/RSBODY/XFERINFO")) {
							RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
							tmpRetDetail.payAct = payAct;
							tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("ACCTTO/ACCTID");
							tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("ACCTTO/NAME");
							tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("TRNAMT");
							tmpRetDetail.result=tmpBkReader.readedXElement.getSingleText("STATUS");
							tmpRetDetail.usage=tmpBkReader.readedXElement.getSingleText("USE");
							tmpRetDetail.postScript=tmpBkReader.readedXElement.getSingleText("REMARK");
							tmpRetDetail.memo=tmpBkReader.readedXElement.getSingleText("ERRMSG");
							tmpRetDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("ERRMSG");
							retAts.writeRetDetail(tmpRetDetail);
						}
					}else if(errorCode!=null){
						retAts.respCode="-1";
						retAts.respInfo=tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/MESSAGE");
						retAts.transState=errorCode+"+"+tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/CODE");
						retAts.payInfoCode=tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/RBATCHTRSFRTRNRS/STATUS/CODE");
						
					} 
					else {
						retAts.respCode = "-1";
						retAts.respInfo = tmpSbMsg.toString();
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			}else if (tmpStrRet[1].indexOf("Connection refused") >= 0) {
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
				retAts.reqSeqID = p_reqQryBat.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = "E9999";
			}
		} catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.getMessage();
			e.printStackTrace();
		}
		if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
			retAts.reqSeqID = p_reqQryBat.reqBatchNO;
		}
		if (retAts.respCode.equals("-1")) {
			retAts.writeBeforeDetail();
		}
		retAts.writeAfterDetail();
		return retAts;
	}

	/**
	 * 批量代扣查询
	 * @param p_reqQryBat
	 * @return
	 * @throws Exception
	 */
	@Override
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_reqQryBat) throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
		XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
		writeCommonQryHeaderEn(xmlWriter, "encoding=\"gb2312\"");
		xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
		xmlWriter.writeStartElement(CIB01TransType.Q9188);
		xmlWriter.writeElementString("TRNUID", p_reqQryBat.reqHeader.transSeqID);
		xmlWriter.directWrite("<RQBODY PAGE=\"1\">");
		xmlWriter.writeElementString("CLIENTREF", p_reqQryBat.reqBatchNO);
		xmlWriter.writeElementString("ACCTID", p_reqQryBat.ownAct);
		xmlWriter.directWrite("</RQBODY>");
		xmlWriter.writeEndDocument();

		String xmlSend = writeContentLength(xmlWriter);
		// 测试代码：打印报文
		// System.out.println("dsp接收报文：" + xmlSend);// 打印返回报文
		String[] tmpStrRet = sendToBank(xmlSend);// 发送银行
		StringBuilder tmpSbMsg = new StringBuilder();
		try {
			if (tmpStrRet[0].length() == 0) {
				String returnXml = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<FOX>"), tmpStrRet[1].length());
				XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
				if (tmpBkReader != null) {
					String retCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHSTMTTRNRS/STATUS/CODE");
					String errorCode=tmpBkReader.getFirstText("/FOX/SIGNONMSGSRSV1/SONRS/STATUS/CODE");
					if (retCode!=null&&retCode.equals("0")) {
						retAts.respCode = "0";
						retAts.payInfoCode = tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHSTMTTRNRS/RSBODY/STATUS");
						retAts.reqSeqID = p_reqQryBat.reqBatchNO;
						retAts.respInfo=tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHSTMTTRNRS/RSBODY/EXECINFO");
						retAts.transState=retCode+"+"+retAts.payInfoCode;
						
						String recAct=tmpBkReader
								.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHSTMTTRNRS/RSBODY/ACCTID");
						retAts.writeBeforeDetail();
						while (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/FBBATCHSTMTTRNRS/RSBODY/CONTENT")) {
							RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
							tmpRetDetail.recAct = recAct;
							tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("ACCTFROM/ACCTID");
							tmpRetDetail.payName = tmpBkReader.readedXElement.getSingleText("ACCTFROM/NAME");
							tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("APPLYAMT");
							tmpRetDetail.result=retAts.transState+"+"+tmpBkReader.readedXElement.getSingleText("RESULT");
							tmpRetDetail.usage=tmpBkReader.readedXElement.getSingleText("PURPOSE");
							//流水号
							tmpRetDetail.postScript=tmpBkReader.readedXElement.getSingleText("MEMO");
							tmpRetDetail.memo=tmpBkReader.readedXElement.getSingleText("MEMO");
							retAts.writeRetDetail(tmpRetDetail);
						}
					}else if(errorCode!=null){
						retAts.respCode="-1";
						retAts.respInfo=tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHSTMTTRNRS/STATUS/MESSAGE");
						retAts.payInfoCode=errorCode+"+"+tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/FBBATCHSTMTTRNRS/STATUS/CODE");
						retAts.transState=retAts.payInfoCode;
					}else {
						retAts.respCode = "-1";
						retAts.respInfo = tmpSbMsg.toString();
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			}else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
				retAts.reqSeqID = p_reqQryBat.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = "E9999";
			}
		} catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.getMessage();
			e.printStackTrace();
		}
		if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
			retAts.reqSeqID = p_reqQryBat.reqBatchNO;
		}
		if (retAts.respCode.equals("-1")) {
			retAts.writeBeforeDetail();
		}
		retAts.writeAfterDetail();
		return retAts;
	}
}
